import 'package:equatable/equatable.dart';

/// 笔记类型枚举
enum NoteType {
  general('GENERAL', '通用'),
  meeting('MEETING', '会议'),
  solution('SOLUTION', '解决方案'),
  issue('ISSUE', '问题');

  const NoteType(this.code, this.description);
  final String code;
  final String description;

  static NoteType fromCode(String code) {
    return NoteType.values.firstWhere(
      (type) => type.code == code,
      orElse: () => NoteType.general,
    );
  }
}

/// 笔记实体类
class Note extends Equatable {
  final int id;
  final int? projectId;
  final int? todoId;
  final String title;
  final String content;
  final NoteType noteType;
  final List<String> tags;
  final int authorId;
  final bool isPublic;
  final DateTime createTime;
  final DateTime updateTime;

  const Note({
    required this.id,
    this.projectId,
    this.todoId,
    required this.title,
    required this.content,
    this.noteType = NoteType.general,
    required this.tags,
    required this.authorId,
    this.isPublic = false,
    required this.createTime,
    required this.updateTime,
  });

  Note copyWith({
    int? id,
    int? projectId,
    int? todoId,
    String? title,
    String? content,
    NoteType? noteType,
    List<String>? tags,
    int? authorId,
    bool? isPublic,
    DateTime? createTime,
    DateTime? updateTime,
  }) {
    return Note(
      id: id ?? this.id,
      projectId: projectId ?? this.projectId,
      todoId: todoId ?? this.todoId,
      title: title ?? this.title,
      content: content ?? this.content,
      noteType: noteType ?? this.noteType,
      tags: tags ?? this.tags,
      authorId: authorId ?? this.authorId,
      isPublic: isPublic ?? this.isPublic,
      createTime: createTime ?? this.createTime,
      updateTime: updateTime ?? this.updateTime,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'projectId': projectId,
      'todoId': todoId,
      'title': title,
      'content': content,
      'noteType': noteType.code,
      'tags': tags.join(','),
      'authorId': authorId,
      'isPublic': isPublic,
      'createTime': createTime.toIso8601String(),
      'updateTime': updateTime.toIso8601String(),
    };
  }

  factory Note.fromJson(Map<String, dynamic> json) {
    return Note(
      id: json['id'] as int,
      projectId: json['projectId'] as int?,
      todoId: json['todoId'] as int?,
      title: json['title'] as String,
      content: json['content'] as String,
      noteType: NoteType.fromCode(json['noteType'] as String? ?? 'GENERAL'),
      tags: (json['tags'] as String?)
              ?.split(',')
              .map((tag) => tag.trim())
              .where((tag) => tag.isNotEmpty)
              .toList() ??
          [],
      authorId: json['authorId'] as int,
      isPublic: json['isPublic'] as bool? ?? false,
      createTime: DateTime.parse(json['createTime'] as String),
      updateTime: DateTime.parse(json['updateTime'] as String),
    );
  }

  @override
  List<Object?> get props => [
        id,
        projectId,
        todoId,
        title,
        content,
        noteType,
        tags,
        authorId,
        isPublic,
        createTime,
        updateTime,
      ];
}

/// 笔记创建请求模型
class NoteCreateRequest extends Equatable {
  final int? projectId;
  final int? todoId;
  final String title;
  final String content;
  final NoteType noteType;
  final List<String> tags;
  final int authorId;
  final bool isPublic;

  const NoteCreateRequest({
    this.projectId,
    this.todoId,
    required this.title,
    required this.content,
    this.noteType = NoteType.general,
    required this.tags,
    required this.authorId,
    this.isPublic = false,
  });

  Map<String, dynamic> toJson() {
    return {
      'projectId': projectId,
      'todoId': todoId,
      'title': title,
      'content': content,
      'noteType': noteType.code,
      'tags': tags.join(','),
      'authorId': authorId,
      'isPublic': isPublic,
    };
  }

  @override
  List<Object?> get props => [
        projectId,
        todoId,
        title,
        content,
        noteType,
        tags,
        authorId,
        isPublic,
      ];
}

/// 笔记更新请求模型
class NoteUpdateRequest extends Equatable {
  final String? title;
  final String? content;
  final NoteType? noteType;
  final List<String>? tags;
  final bool? isPublic;

  const NoteUpdateRequest({
    this.title,
    this.content,
    this.noteType,
    this.tags,
    this.isPublic,
  });

  Map<String, dynamic> toJson() {
    return {
      if (title != null) 'title': title,
      if (content != null) 'content': content,
      if (noteType != null) 'noteType': noteType!.code,
      if (tags != null) 'tags': tags!.join(','),
      if (isPublic != null) 'isPublic': isPublic,
    };
  }

  @override
  List<Object?> get props => [
        title,
        content,
        noteType,
        tags,
        isPublic,
      ];
}